package com.taotao.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.portal.pojo.CartItem;
import com.taotao.portal.service.CartService;

@Controller
@RequestMapping("/cart")
public class CartContorller {
	
	@Autowired
	private CartService cartService;
	
	@RequestMapping("/clear")
	@ResponseBody
	public TaotaoResult clear(HttpServletRequest request,HttpServletResponse response) {
		return cartService.clearCart(request, response);
	}
	
	
	@RequestMapping("/cart")
	public String cart(HttpServletRequest request,HttpServletResponse response,Model model) {
		
		List<CartItem> cart = cartService.getCart(request);
		model.addAttribute("cartList", cart);
		return "cart";
	}
	
	@RequestMapping("/add/{id}")
	public String addItemToCart(@PathVariable Long id,@RequestParam(defaultValue = "1") Integer num,Model model,
			HttpServletRequest request,HttpServletResponse response) {
		if(id==null) {
			model.addAttribute("message", "添加失败");
			return "error/exception";
		}
		try {
			TaotaoResult result=cartService.addCartItem(id, num, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message", "添加失败");
			return "error/exception";
		}
		return "redirect:/cart/success.html";
	}
	
	@RequestMapping("/update/num/{id}")
	@ResponseBody
	public TaotaoResult updataItemToCart(@PathVariable Long id,@RequestParam(defaultValue = "1") Integer num,Model model,
			HttpServletRequest request,HttpServletResponse response) {
		if(id==null) {
			return TaotaoResult.build(400, "错误的参数");
		}
		try {
			TaotaoResult result=cartService.updataCartItem(id, num, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message", "添加失败");
			return TaotaoResult.build(500, "添加失败");
		}
		return TaotaoResult.ok();
	}
	
	@RequestMapping("/del/{id}")
	@ResponseBody
	public TaotaoResult delItemToCart(@PathVariable Long id,Model model,
			HttpServletRequest request,HttpServletResponse response) {
		if(id==null) {
			return TaotaoResult.build(400, "错误的参数");
		}
		try {
			TaotaoResult result=cartService.delCartItem(id, request, response);
		} catch (Exception e) {
			e.printStackTrace();
			model.addAttribute("message", "添加失败");
			return TaotaoResult.build(500, "添加失败");
		}
		return TaotaoResult.ok();
	}
	
	
	@RequestMapping("/success")
	public String redirectSuccess() {
		return "cartSuccess";
	}
}
